/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ContactListFrame.java
 *
 * Created on 20 févr. 2009, 23:20:39
 */

package org.wisigoth.chat.swing.accountframe;

import java.util.Collection;

import javax.swing.DefaultComboBoxModel;

import org.wisigoth.chat.client.Contact;
import org.wisigoth.chat.client.DefaultPresence;
import org.wisigoth.chat.client.Presence;
import org.wisigoth.chat.client.PresenceEvent;
import org.wisigoth.chat.client.PresenceListener;
import org.wisigoth.chat.client.Wisigoth;
import org.wisigoth.chat.swing.WisigothController;

/**
 * 
 * @author dtfs-tof
 */
@SuppressWarnings("serial")
public class ContactListFrame extends javax.swing.JFrame {

	

	private WisigothController ctrl;

	/** Creates new form ContactListFrame */
	public ContactListFrame(WisigothController controller) {
		this.ctrl = controller;
		this.initComponents();
	}

	
	/**
	 * This method is called from within the constructor to initialize the form.
	 * WARNING: Do NOT modify this code. The content of this method is always
	 * regenerated by the Form Editor.
	 */
	@SuppressWarnings("unchecked")
	// <editor-fold defaultstate="collapsed"
	// desc="Generated Code">//GEN-BEGIN:initComponents
	private void initComponents() {
		Presence lastPresence = this.ctrl.getSession().getWisigoth().getUserPreferences().getLastPresence();
		this.ctrl.getSession().setPresence(lastPresence);
		Collection<Contact> contacts = ContactListFrame.this.ctrl.getSession().getFriendList().whoIKnow();
		Collection<Presence> presences = DefaultPresence.getDefaults();
		Object[] presencesArray = presences.toArray();
		final DefaultComboBoxModel presenceComboBoxModel = new DefaultComboBoxModel(
				presencesArray);
		Wisigoth wisigoth = this.ctrl.getSession().getWisigoth();
		presenceComboBoxModel.setSelectedItem(lastPresence);
		
		this.setIconImage(lastPresence.getPresenceImage().getImage());
		this.ctrl.getSession().addPresenceListener(new PresenceListener() {

			@Override
			public void presenceChanged(PresenceEvent event) {
				Presence presence = event.getSource().getPresence();
				presenceComboBoxModel.setSelectedItem(presence);
				ContactListFrame.this.setIconImage(presence.getPresenceImage()
						.getImage());
			}

		});

		java.awt.GridBagConstraints gridBagConstraints;

		this.rootlPanel = new javax.swing.JPanel();
		this.contactListPanel = new javax.swing.JPanel();
		this.jScrollPane1 = new javax.swing.JScrollPane();
		this.contactList = new javax.swing.JList();
		this.contactList.setCellRenderer(new ContactListCellRenderer());
		this.presenceStates = new javax.swing.JList();
		this.presenceStates.setCellRenderer(new PresenceCellListRenderer());
		this.presenceComboBox = new javax.swing.JComboBox();
		this.presenceStates.setListData(presences.toArray());
		this.presenceComboBox.setModel(presenceComboBoxModel);
		
		this.presencePanel = new javax.swing.JPanel();

		this
				.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
		this.getContentPane().setLayout(new java.awt.GridBagLayout());

		this.rootlPanel.setLayout(new java.awt.GridBagLayout());

		this.contactListPanel.setLayout(new java.awt.GridBagLayout());

		this.jScrollPane1.setViewportView(this.contactList);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		this.contactListPanel.add(this.jScrollPane1, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		this.rootlPanel.add(this.contactListPanel, gridBagConstraints);

		this.presencePanel.setLayout(new java.awt.GridBagLayout());

		this.presenceComboBox.setRenderer(new PresenceCellListRenderer());
		// presenceComboBox.setModel(new javax.swing.DefaultComboBoxModel(new
		// String[] { "Available", "Away", "Do Not Disturb", "Offline" }));
		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		gridBagConstraints.weightx = 1.0;
		this.presencePanel.add(this.presenceComboBox, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 1;
		gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
		this.rootlPanel.add(this.presencePanel, gridBagConstraints);

		gridBagConstraints = new java.awt.GridBagConstraints();
		gridBagConstraints.gridx = 0;
		gridBagConstraints.gridy = 0;
		gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
		gridBagConstraints.weightx = 1.0;
		gridBagConstraints.weighty = 1.0;
		this.getContentPane().add(this.rootlPanel, gridBagConstraints);

		this.contactListMenu = new javax.swing.JMenuBar();
		this.accountMenu = new javax.swing.JMenu();
		this.messagingMenuItem = new javax.swing.JMenuItem();
		this.changeUserMenuItem = new javax.swing.JMenuItem();
		this.addContactMenuItem = new javax.swing.JMenuItem();
		this.QuitMenuItem = new javax.swing.JMenuItem();
		this.helpMenu = new javax.swing.JMenu();
		this.helpMenuItem = new javax.swing.JMenuItem();
		this.aboutMenuItem = new javax.swing.JMenuItem();

		this.accountMenu.setText("Account");

		this.messagingMenuItem.setText("Start Messaging...");
		this.accountMenu.add(this.messagingMenuItem);

		this.changeUserMenuItem.setText("Change User...");
		this.accountMenu.add(this.changeUserMenuItem);

		this.addContactMenuItem.setText("Add Contact...");

		this.accountMenu.add(this.addContactMenuItem);

		this.QuitMenuItem.setAccelerator(javax.swing.KeyStroke.getKeyStroke(
				java.awt.event.KeyEvent.VK_Q,
				java.awt.event.InputEvent.CTRL_MASK));
		this.QuitMenuItem.setText("Quit...");
		this.accountMenu.add(this.QuitMenuItem);

		this.contactListMenu.add(this.accountMenu);

		this.helpMenu.setText("Help");

		this.helpMenuItem.setText("Help");
		this.helpMenu.add(this.helpMenuItem);

		this.aboutMenuItem.setText("About");
		this.helpMenu.add(this.aboutMenuItem);

		this.contactListMenu.add(this.helpMenu);

		this.setJMenuBar(this.contactListMenu);
		ContactListFrame.this.contactList.setListData(contacts.toArray());
		
		this.setLocationByPlatform(true);
		
		this.pack();
		this.setSize(250, 400);
	}// </editor-fold>//GEN-END:initComponents

	/**
	 * @param args
	 *            the command line arguments
	 */

	// Variables declaration - do not modify//GEN-BEGIN:variables
	private javax.swing.JPanel contactListPanel;
	private javax.swing.JComboBox presenceComboBox;
	private javax.swing.JList contactList;
	private javax.swing.JList presenceStates;
	private javax.swing.JMenuItem QuitMenuItem;
	private javax.swing.JMenuItem aboutMenuItem;
	private javax.swing.JMenu accountMenu;
	private javax.swing.JMenuItem addContactMenuItem;
	private javax.swing.JMenuItem changeUserMenuItem;
	private javax.swing.JMenuBar contactListMenu;
	private javax.swing.JMenu helpMenu;
	private javax.swing.JMenuItem helpMenuItem;
	private javax.swing.JMenuItem messagingMenuItem;
	private javax.swing.JScrollPane jScrollPane1;
	private javax.swing.JPanel presencePanel;
	private javax.swing.JPanel rootlPanel;

	// End of variables declaration//GEN-END:variables

	public javax.swing.JComboBox getPresenceComboBox() {
		return this.presenceComboBox;
	}

	public javax.swing.JMenuItem getQuitmenuItem() {
		return this.QuitMenuItem;
	}

	public javax.swing.JList getJList() {
		return this.contactList;
	}

}
